package cn.kgc.web.controller;

import cn.kgc.common.entity.Article;
import cn.kgc.common.entity.Author;
import cn.kgc.common.entity.Category;
import cn.kgc.common.entity.DataPage;
import cn.kgc.web.service.ArticleService;
import cn.kgc.web.service.CommentService;
import cn.kgc.web.service.CountService;
import cn.kgc.web.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by teacher ZHANG on 2023/2/1
 */
@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private CountService countService;
    @Autowired
    private SearchService searchService;

    @RequestMapping("/")
    public String index(@CookieValue(value = "token", required = false) String token,Model model) {
        List<Category> categories = articleService.listCategories();
        List<Map<String, Object>> maps = countService.listTopCounts(5);

        List<Integer> idList = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            idList.add((Integer) map.get("articleId"));
        }


        List<Article> articles = new ArrayList<>();

        //根据id取出所有文章
        List<Article> list = articleService.listByIds(idList);
        for (Map<String, Object> map : maps) {
            Integer aid = (Integer) map.get("articleId");
            Integer num = (Integer) map.get("visitorNum");

            for (Article article : list) {
                if (article.getArticleId().equals(aid)) {
                    article.setVisitorNum(num);
                    articles.add(article);
                    break;
                }
            }
        }

        model.addAttribute("categories", categories);
        model.addAttribute("articles", articles);

        return "article_index";
    }

    @RequestMapping("/article/list_articles")
    @ResponseBody
    public DataPage<HashMap<String, Object>> listArticles(Integer categoryId, Integer pageNum, Integer pageSize) {
        Integer page = pageNum==null? 1 : pageNum;
        Integer size = pageSize==null? 5 : pageSize;
        Integer cid = categoryId==null? 0 : categoryId;

        DataPage<HashMap<String, Object>> dataPage = articleService.listArticles(cid, page, size);

        List<Integer> idList = new ArrayList<>();
        for (Map<String, Object> map : dataPage.getDataList()) {
            idList.add((Integer) map.get("articleId"));
        }

        //取评论量
        List<Map<String, Object>> comments = commentService.listByArticleIds(idList);
        for (Map<String, Object> map : dataPage.getDataList()) {
            map.put("commentNum", 0);  //初始化为0

            for (Map<String, Object> c : comments) {
                Integer commentNum = (Integer) c.get("commentNum");

                if (map.get("articleId").equals(c.get("articleId"))) {
                    map.put("commentNum", commentNum);
                }
            }
        }

        //取浏览量
        List<Map<String, Object>> counts = countService.listByArticleIds(idList);
        for (Map<String, Object> map : dataPage.getDataList()) {
            map.put("visitorNum", 0);  //初始化为0

            for (Map<String, Object> c : counts) {
                Integer visitorNum = (Integer) c.get("visitorNum");

                if (map.get("articleId").equals(c.get("articleId"))) {
                    map.put("visitorNum", visitorNum);
                }
            }
        }

        return dataPage;
    }

    @RequestMapping("/article/get_detail")
    public String getDetail(Integer articleId, @CookieValue(value = "token", required = false) String token,
                            HttpServletRequest request, Model model) {
        //取远程终端的ip地址
        String address = request.getRemoteAddr();
        //System.out.println(address);
        //加入redis中进行统计
        Integer count = countService.addAddress(articleId, address);

        Article article = articleService.findArticle(articleId);
        article.setVisitorNum(count);
/*
        //查看用户有无登录
        if (token != null && userService.tokenExist(token)) {
            Author user = userService.findAuthor(token);
            model.addAttribute("nickname", user.getNickname());
        }
*/
        model.addAttribute("article", article);

        return "article_detail";
    }

    @RequestMapping("/article/search")
    public String searchArticles(String keyword, String fromDate, String endDate, Integer pageNum, Model model) {
        Integer page = pageNum==null? 1 : pageNum;

        //起止时间为当前时间和前一周或一月...
        List<Article> articles = searchService.searchArticles(keyword, "2020-05-01", "2020-06-01", page);

        model.addAttribute("keyword", keyword);
        model.addAttribute("articles", articles);

        return "article-list";
    }
}
